﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using CommandBinding;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabMeasurement.xaml
    /// </summary>
    public partial class VMTabMeasurement : UserControl
    {
        public VMTabMeasurement()
        {
            InitializeComponent();            
            SetupShowDetail();
            SetupCommandBinding();           
        }

        private void SetupCommandBinding()
        {
            this.AssignCondition(Button_Remove, (s, e) =>
                {
                    Button_Remove_Click();
                }, (s, e) =>
                    {
                        e.OnCanExecute_SyncButtonVisibility(true, ListView_Measurement.SelectedItem != null);
                    });
        }

        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Action_ShowDetail(null);
        }

        private void Button_Remove_Click()
        {            
            Step_Measurement step = this.DataContext as Step_Measurement;
            if (step != null)
            {
                var removeData = ListView_Measurement.SelectedItem as Step_Measurement_Item;
                step.Measurements_Source.Remove(removeData);
            }
        }

        
        #region SetupShowDetail
        private void SetupShowDetail()
        {
            ListView_Measurement.MouseDoubleClick -= new MouseButtonEventHandler(ListView_Measurement_MouseDoubleClick);
            ListView_Measurement.MouseDoubleClick += new MouseButtonEventHandler(ListView_Measurement_MouseDoubleClick);
            Button_Detail.Click -= new RoutedEventHandler(Button_Detail_Click);
            Button_Detail.Click += new RoutedEventHandler(Button_Detail_Click);
            Button_SortTile.Click -= new RoutedEventHandler(Button_SortTile_Click);
            Button_SortTile.Click += new RoutedEventHandler(Button_SortTile_Click);
            Button_SortStart.Click -= new RoutedEventHandler(Button_SortStart_Click);
            Button_SortStart.Click += new RoutedEventHandler(Button_SortStart_Click);
            Button_SortDuration.Click -= new RoutedEventHandler(Button_SortDuration_Click);
            Button_SortDuration.Click += new RoutedEventHandler(Button_SortDuration_Click);
            Button_SortDaysRemaining.Click -= new RoutedEventHandler(Button_SortDaysRemaining_Click);
            Button_SortDaysRemaining.Click += new RoutedEventHandler(Button_SortDaysRemaining_Click);
        }       
        void Button_Detail_Click(object sender, RoutedEventArgs e)
        {
            Action_ShowDetail();
        }
        void Button_SortTile_Click(object sender, RoutedEventArgs e)
        {
            Step_Measurement step = this.DataContext as Step_Measurement;
            if (step != null)
            {
                step.Measurements_Source_SortBy_Name();
            }
        }
        void Button_SortStart_Click(object sender, RoutedEventArgs e)
        {
            Step_Measurement step = this.DataContext as Step_Measurement;
            if (step != null)
            {
                step.Measurements_Source_SortBy_StartDate();
            }
        }
        void Button_SortDuration_Click(object sender, RoutedEventArgs e)
        {
            Step_Measurement step = this.DataContext as Step_Measurement;
            if (step != null)
            {
                step.Measurements_Source_SortBy_Duration();
            }
        }
        void Button_SortDaysRemaining_Click(object sender, RoutedEventArgs e)
        {
            Step_Measurement step = this.DataContext as Step_Measurement;
            if (step != null)
            {
                step.Measurements_Source_SortBy_DaysRemaining();
            }
        }
        void ListView_Measurement_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Action_ShowDetail();
        }
        void Action_ShowDetail()
        {
            if (ListView_Measurement.SelectedItem != null)
            {
                var task = ListView_Measurement.SelectedItem as Step_Measurement_Item;
                Action_ShowDetail(task);
            }
        }

        private void Action_ShowDetail(Step_Measurement_Item task)
        {
            VMTabMeasurement_Detail detail = new VMTabMeasurement_Detail();//{ DataContext = ListView_Measurement.SelectedItem };
            detail.Background = this.Background;
            detail.SetupDataContext(task);
            detail.Action_AddNew = (newData) =>
                {
                    Step_Measurement step = this.DataContext as Step_Measurement;
                    if (step != null)
                    {
                        step.Measurements_Source.Add(newData);
                    }
                };
            Window w = new Window()
            {
                Icon = Application.Current.MainWindow.Icon,
                Content = detail
                    //, SizeToContent = SizeToContent.WidthAndHeight
                ,
                Title = "Measurement"
                ,                 
                    WindowState = WindowState.Normal
                    , WindowStartupLocation = WindowStartupLocation.CenterScreen
                    ,
                    Height = System.Windows.SystemParameters.PrimaryScreenHeight * 8 / 9
                ,
                    Width = System.Windows.SystemParameters.PrimaryScreenWidth * 4 / 9
                    ,
                Background = new SolidColorBrush(Color.FromArgb(0xff,0xEE,0xEE,0xF2))
            };
            //w.Width =
            w.ShowDialog();
        }
        #endregion
    }
}
